//
//  YWMaterialRecordApplyBillHeadCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/9/19.
//  Copyright © 2016年 ewin. All rights reserved.
//



#import "YWMaterialRecordApplyBillHeadCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"

#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"
#import "YWHandler.h"
#import "YYLabel.h"
#import "YWContactManager.h"
#import "YWMaterialApplyDetail.h"
#import "YWMaterialStock.h"
#import "YWMaterial.h"
#import "YWReply.h"

@interface YWMaterialRecordApplyBillHeadCell()
/** 单名 */
@property (nonatomic, strong) UIView *letfLine;
@property (nonatomic, strong) UIView *rightLine;
@property (nonatomic, strong) YYLabel *materialNameLabel;
/**  申请单号 */
@property (nonatomic, strong) YYLabel *applyNumbersLabel;
/** 申请人名称 */
@property (nonatomic, strong) YYLabel *applyNameLabel;
/**  接收人 */
@property (nonatomic, strong) YYLabel *receivedManNameLabel;
/**  申请时间 */
@property (nonatomic, strong) YYLabel *applyDateLabel;
/** 用途 */
@property (nonatomic, strong) YYLabel * noteLabel;
/** 物料清单 */
//@property (nonatomic, strong) YYLabel * listMaterialLabel;
///** 清单 */
//@property (nonatomic, strong) YYLabel * listLabel;
///** 评论 */
//@property (nonatomic, strong) YYLabel * replyLabel;
///** 物料数量 */
//@property (nonatomic, strong) UIButton * materialNumbel;
///** 评论数量 */
//@property (nonatomic, strong) UIButton * replyNumbel;
@end

@implementation YWMaterialRecordApplyBillHeadCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubviews{
    /**
     *  ------ 单名 -------
     */
    [self.materialNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).offset(8);
    }];
    [self.letfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(_materialNameLabel.mas_left).offset(-8);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(@[_materialNameLabel,_letfLine]);
        make.left.mas_equalTo(_materialNameLabel.mas_right).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.height.mas_equalTo(@[@1,_letfLine]);
    }];
    /**  申请单号 */
    [self.applyNumbersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_materialNameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 申请人 */
    [self.applyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNumbersLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 接收人 */
    [self.receivedManNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 申请日期 */
    [self.applyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 用途 */
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyDateLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    [self.separatorLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(@1.0);
    }];
    [self.materialsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.separatorLineView.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
    }];
    [self.observersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.materialsLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
    }];
    [self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.observersLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
        make.bottom.mas_equalTo(self.contentView).offset(-6);
    }];
//    /* 评论数量 */
//    [self.replyNumbel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.right.mas_equalTo(self.contentView).offset(-15);
//        make.width.mas_equalTo(@46);
//        make.bottom.mas_equalTo(self.contentView).offset(-6);
//    }];
//    /* 评论 */
//    [self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.right.mas_equalTo(_replyNumbel.mas_left).offset(-2);
//        make.bottom.mas_equalTo(self.contentView).offset(-6);
//    }];
//    /* 物料数量 */
//    [self.materialNumbel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.right.mas_equalTo(_replyLabel.mas_left).offset(-10);
//        make.width.mas_equalTo(@46);
//        make.bottom.mas_equalTo(self.contentView).offset(-6);
//    }];
//    /* 清单 */
//    [self.listLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.right.mas_equalTo(_materialNumbel.mas_left).offset(-2);
//        make.bottom.mas_equalTo(self.contentView).offset(-6);
//    }];
    /* 物料清单名称 */
    //    [self.listMaterialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
    //        make.left.mas_equalTo(self.contentView).offset(8);
    //        make.right.mas_equalTo(self.contentView).offset(-8);
    //        make.bottom.mas_equalTo(self.contentView).offset(-6);
    //    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (void)setApply:(YWMaterialApplyViewModel *)apply
{
    _apply = apply;
    _materialNameLabel.text = @"申请单";
    
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.applySequence secondColor:[UIColor customYellowColor] font:kContentFont];
    _applyNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.creator.userName?:@"未知用户" secondColor:[UIColor customYellowColor] font:kContentFont];
    
    NSMutableString * receipientString = [NSMutableString string];
    [_apply.receivers enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [receipientString appendString:@","];
        }
        [receipientString appendString:[NSString stringWithFormat:@"%@",handler.userName]];
    }];
    
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:receipientString secondColor:[UIColor blackColor] font:kContentFont];
//    NSMutableString * copierString = [NSMutableString string];
//    [_apply.copies enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (idx!=0) {
//            [copierString appendString:@","];
//        }
//        [copierString appendString:[NSString stringWithFormat:@"%@",handler.userName?:@"无"]];
//    }];
//    if (copierString.length>0) {
//        [_copiedManLabel labelText:@"抄送人: " withAttributedText:copierString andAttributedColor:[UIColor customYellowColor]];
//    }else{
//        [_copiedManLabel labelText:@"抄送人: " withAttributedText:@"无" andAttributedColor:[UIColor customYellowColor]];
//    }
    
    _applyDateLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请时间: " firstColor:[UIColor customTextGrayColor] secondText:[YWCommonTool showTimeWithNormalFormatter:_apply.applyTime /1000] secondColor:[UIColor blackColor] font:kContentFont];
    if (_apply.note.length>0) {
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.note secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    }
    
    //申请人、接收人、参与人
    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *observerArray = [NSMutableArray array];
    [tempArray addObject:_apply.creator];
    if (_apply.rejectUser) {
        [tempArray addObject:_apply.rejectUser];
    }
    [tempArray addObjectsFromArray:_apply.receivers];
    [tempArray addObjectsFromArray:_apply.observers];
    for (YWHandler *handler in tempArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[observerArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [observerArray addObject:handler];
        }
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)observerArray.count];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    [observerArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",observerName?:@""];//参与者要加进来
    
    self.observersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    
    //物料
    NSMutableString *materialNames = [[NSMutableString alloc] init];
    [self.apply.details enumerateObjectsUsingBlock:^(YWMaterialApplyDetail*  _Nonnull applyDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (materialNames.length && idx!=0) {
            [materialNames appendString:@" "];
        }
        NSString * nameNumber = [NSString stringWithFormat:@"%@*%ld",applyDetail.materialStock.materialInfo.materialName,applyDetail.quantity];;
        [materialNames appendString:nameNumber];
    }];
    NSString * materialCount = [NSString stringWithFormat:@"物料(%ld): ",(long)self.apply.details.count];
    self.materialsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:materialCount firstColor:[UIColor customTextGrayBlackColor] secondText:materialNames secondColor:[UIColor customBlueColor] font:kContentFont];
    
    //评论
    if (self.apply.replies.count>0) {
        YWReply * reply = [self.apply.replies lastObject];
        NSString * replyCount = [NSString stringWithFormat:@"评论(%ld): ",self.apply.replies.count];
        //    时间
        NSString * timeStr = [YWCommonTool recordDateByFormatTimeString:reply.replyTime/1000 showYear:YES];
        NSString * timeText = [NSString stringWithFormat:@"  %@",timeStr];
        //    内容
        NSString *replyName = [NSString stringWithFormat:@"%@: ",reply.replier.userName];
        NSString *atUserName = reply.atUser.userName.length>0?[NSString stringWithFormat:@" @%@",reply.atUser.userName]:@"";
        NSString * content = atUserName.length?[NSString stringWithFormat:@" %@  ",reply.content.length>0?reply.content:@""] : (reply.content.length>0?reply.content:@"");
        
        self.replyLabel.attributedText = [YWCommonTool attributedTextWithFirstText:replyCount firstColor:[UIColor customTextGrayColor] firstFont:kContentFont secondText:replyName secondColor:[UIColor customBlueColor] secFont:kContentFont thirdText:atUserName thirdColor:[UIColor customBlueColor] thirdFont:kContentFont fourthText:content fourthColor:[UIColor blackColor] fourFont:kContentFont fifthText:timeText fifthColor:[UIColor customTextGrayColor] fifthFont:[UIFont systemFontOfSize:13]];
    }else{
        self.replyLabel.attributedText = nil;
    }
//    _replyLabel.text = @"评论";
//    _listLabel.text = @"清单";
    
//    [_replyNumbel setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)_apply.replies.count] forState:UIControlStateNormal];
//    [_materialNumbel setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)_apply.details.count] forState:UIControlStateNormal];
    //    _listMaterialLabel.text = @"物料申请清单";
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.materialsLabel.preferredMaxLayoutWidth = self.materialsLabel.frame.size.width;
    self.replyLabel.preferredMaxLayoutWidth = self.replyLabel.frame.size.width;
    self.observersLabel.preferredMaxLayoutWidth = self.observersLabel.frame.size.width;
}

- (YYLabel *)materialNameLabel{
    if (!_materialNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _materialNameLabel = label;
        [self.contentView addSubview:_materialNameLabel];
    }
    return _materialNameLabel;
}

- (UIView *)letfLine{
    if (!_letfLine) {
        _letfLine = [UIView new];
        _letfLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_letfLine];
    }
    return _letfLine;
}

- (UIView *)rightLine{
    if (!_rightLine) {
        _rightLine = [UIView new];
        _rightLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_rightLine];
    }
    return _rightLine;
}

- (YYLabel *)applyNumbersLabel{
    if (!_applyNumbersLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNumbersLabel = label;
        [self.contentView addSubview:_applyNumbersLabel];
    }
    return _applyNumbersLabel;
}

- (YYLabel *)applyNameLabel{
    if (!_applyNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNameLabel = label;
        [self.contentView addSubview:_applyNameLabel];
    }
    return _applyNameLabel;
}

- (YYLabel *)receivedManNameLabel{
    if (!_receivedManNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _receivedManNameLabel = label;
        [self.contentView addSubview:_receivedManNameLabel];
    }
    return _receivedManNameLabel;
}

//- (YYLabel *)copiedManLabel{
//    if (!_copiedManLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.numberOfLines = 0;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _copiedManLabel = label;
//        [self.contentView addSubview:_copiedManLabel];
//    }
//    return _copiedManLabel;
//}

- (YYLabel *)applyDateLabel{
    if (!_applyDateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyDateLabel = label;
        [self.contentView addSubview:_applyDateLabel];
    }
    return _applyDateLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

//- (YYLabel *)listMaterialLabel{
//    if (!_listMaterialLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _listMaterialLabel = label;
//        [self.contentView addSubview:_listMaterialLabel];
//    }
//    return _listMaterialLabel;
//}

//- (YYLabel *)listLabel{
//    if (!_listLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _listLabel = label;
//        [self.contentView addSubview:_listLabel];
//    }
//    return _listLabel;
//}

//- (YYLabel *)replyLabel{
//    if (!_replyLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _replyLabel = label;
//        [self.contentView addSubview:_replyLabel];
//    }
//    return _replyLabel;
//}

//- (UIButton *)materialNumbel{
//    if (!_materialNumbel) {
//        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setImage:[UIImage imageNamed:@"material_list"] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        button.titleLabel.font = KTitleFont;
//        _materialNumbel = button;
//        [self.contentView addSubview:_materialNumbel];
//    }
//    return _materialNumbel;
//}
//
//- (UIButton *)replyNumbel{
//    if (!_replyNumbel) {
//        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setImage:[UIImage imageNamed:@"remind_replyIcon"] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        button.titleLabel.font = KTitleFont;
//        _replyNumbel = button;
//        [self.contentView addSubview:_replyNumbel];
//    }
//    return _replyNumbel;
//}

@end
